package com.jiyun.user.service.impl;

import cn.hutool.json.JSONUtil;
import com.jiyun.common.pojo.home.User;
import com.jiyun.common.pojo.user.Integral;
import com.jiyun.common.pojo.user.SigninRecord;
import com.jiyun.user.mapper.IntegralMapper;
import com.jiyun.user.mapper.SigninRecordMapper;
import com.jiyun.user.service.IntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IntegralServiceImpl implements IntegralService {
    @Autowired
    private IntegralMapper integralMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SigninRecordMapper signinRecordMapper;

    @Override
    public Integral findIntegral(String token) {
        if (token!=null){
            //根据token获取用户信息
            Object o = redisTemplate.opsForValue().get(token);
            if (o==null){
                return null;
            }
            //将用户信息转换为User对象
            User user = JSONUtil.toBean(o.toString(), User.class);
            //查询积分信息
            Integral integral = integralMapper.findIntegral(user.getId());
            List<SigninRecord> signinRecords = signinRecordMapper.findSigninRecord(user.getId());
            integral.setSigninRecords(signinRecords);
            return integral;
        }
        return null;
    }
}
